﻿using System;

namespace HCMS.Utils
{
    /// <summary>
    /// 日期时间 工具类
    /// </summary>
    public static class DateTimeUtil
    {
        #region GetTimestamp 获取时间戳
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetTimestamp()
        {
            return DateTimeOffset.Now.ToUnixTimeSeconds();
        }
        #endregion

        #region GetWeekNumber 获取指定时间是当年的第几周
        /// <summary>
        /// 获取指定时间是当年的第几周
        /// </summary>
        /// <remarks>
        /// 例如：2024-06-27 是 2024 年的第 26 周
        /// </remarks>
        /// <param name="dateTime">时间</param>
        /// <returns></returns>
        public static int GetWeekNumber(DateTime dateTime)
        {
            int result = new System.Globalization.GregorianCalendar().GetWeekOfYear(dateTime, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
            return result;
        }
        #endregion

        #region DifferenTimespan 获取时间区间跨度
        /// <summary>
        /// 获取时间区间跨度
        /// </summary>
        /// <param name="startTime">开始 时间</param>
        /// <param name="endTime">结束 时间</param>
        /// <returns></returns>
        public static TimeSpan DifferenTimespan(DateTimeOffset startTime, DateTimeOffset endTime)
        {
            return new TimeSpan(endTime.Ticks - startTime.Ticks);
        }
        #endregion

        #region ExecutionTimeStr 耗时中文字符串表示
        /// <summary>
        /// 耗时中文字符串表示
        /// </summary>
        /// <param name="executionTime">耗时。单位：毫秒</param>
        /// <returns></returns>
        public static string ExecutionTimeStr(int executionTime)
        {
            string result = string.Empty;
            if (executionTime < 1000)
            {
                result = $"{executionTime} 毫秒";
            }
            else if (executionTime > (1 * 1000) & executionTime <= (60 * 1000))
            {
                result = $"{(float)(executionTime / 1000)} 秒";
            }
            else if (executionTime > (1 * 60 * 1000) & executionTime <= (1 * 60 * 60 * 1000))
            {
                result = $"{(float)(executionTime / 60 / 1000)} 分钟";
            }
            else if (executionTime > (1 * 60 * 60 * 1000) & executionTime <= (1 * 60 * 60 * 24 * 1000))
            {
                result = $"{(float)(executionTime / 60 / 60 / 1000)} 小时";
            }
            else
            {
                result = executionTime.ToString();
            }

            return result;
        }
        #endregion

        #region 本日
        /// <summary>
        /// 将日期转换为本日的开始时间
        /// </summary>
        /// <param name="value">2025-01-01</param>
        /// <returns>2025-01-01 00:00:00</returns>
        public static DateTime ToDayStart(DateTime value)
        {
            string shortDate = value.ToShortDateString();
            return DateTime.Parse(shortDate);
        }

        /// <summary>
        /// 将日期转换为本日的结束时间
        /// </summary>
        /// <param name="value">2025-01-01</param>
        /// <returns>2025-01-01 23:59:59</returns>
        public static DateTime ToDayEnd(DateTime value)
        {
            string shortDate = value.ToShortDateString();
            DateTime date = DateTime.Parse(shortDate);
            return date.AddDays(1).AddSeconds(-1);
        }
        #endregion

        #region 本周

        #endregion

        #region 本月

        #endregion
    }
}
